from django.shortcuts import render_to_response
from django.template import RequestContext
from muser.models import * 
from django.views.generic import list_detail
from django.shortcuts import get_object_or_404

def user_info(request):
	'''
	muser home page
	'''
	if request.user.is_authenticated():
		_u = User.objects.get(username=request.user.username)
		_u_full_name = _u.first_name + ', ' + _u.last_name
		_u_nickname = _u.username
		_u_email = _u.email
		_u_profile = _u.userprofile
		_u_books = _u_profile.reading_books.all()
		context = {
			'nickname': _u_nickname,
			'email': _u_email,
			'full_name': _u_full_name,
			'profile': _u_profile,
		}
	else:
		context = {}

	return render_to_response('muser/user_info.html', context, RequestContext(request))

def user_check(request):
	'''
	Check reserving messages
	'''
	i = int(request.GET.get('i', '0'))
	if i:
		reserving = get_object_or_404(Reserving, pk=i)
		Reading.objects.get_or_create(user_reading=reserving.user_reserving,
			book_read=reserving.book_reserved)
		reserving.delete()
	
	o = int(request.GET.get('o', '0'))
	if o:
		reading = get_object_or_404(Reading, pk=o)
		reading.delete()
		
	return list_detail.object_list(
		request,
		queryset = Reserving.objects.filter(status=1),
		template_name = 'muser/user_check.html',
		# template_object_name = 'reservings_list',
		extra_context = { 'giving_backs_list': Reading.objects.filter(status=0) }
	)
